package com.encuestas;

import java.util.List;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.BarChart.Type;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.SimpleSeriesRenderer;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import com.bd.dal;
import com.bd.estadistica;
import com.medios.imark.encuesta.R;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint.Align;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;

public class barGrafico extends Activity {
	

	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.barchart);
		generateChart();
	}

	
	
	public void generateChart() {
		dal  dto= new dal(this);
		List<estadistica> resul= dto.getEstadisticasCompletadas();
		GraphicalView graf;
		int max=0;
		LinearLayout vista =(LinearLayout)findViewById(R.id.grafic);
		int [] numEncuestas= new int [resul.size()];
		String []fechas = new String[resul.size()];
		if (resul.size()> 0)
		{
			for (int i = 0; i< resul.size(); i++)
			{
				estadistica t = resul.get(i);
				numEncuestas[i]=t.getConteoYerminadas();
				fechas[i]= t.fechaFormatCorto();
				if(max<t.getConteoYerminadas())
				{
					max=t.getConteoYerminadas();
				}
			}
		}
		
		XYSeries xy = new XYSeries("Encuestas terminadas");
		for (int j = 0;j< resul.size(); j ++)
		{
			xy.add(j, numEncuestas[j]);
		}
		 XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
		 dataset.addSeries(xy);
		
		 XYSeriesRenderer xyestilo = new XYSeriesRenderer();
		 xyestilo.setColor(Color.rgb(130, 130, 230));
		 xyestilo.setFillPoints(true);
		 xyestilo.setDisplayChartValues(true);
		 xyestilo.setChartValuesTextSize(18);
		 xyestilo.setChartValuesTextAlign(Align.RIGHT);
		
		 XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();
		// multiRenderer.setXLabels(0);
		 
		    multiRenderer.setChartTitle("Encuestas terminadas");
		    multiRenderer.setXTitle("fecha");
		    multiRenderer.setYTitle("numero de encuestas");
		    multiRenderer.setXAxisMin(-0.5);
		    multiRenderer.setXAxisMax(fechas.length );
		    multiRenderer.setBackgroundColor(Color.DKGRAY);
		  multiRenderer.setXLabelsAlign(Align.CENTER);
		  multiRenderer.setYLabelsAlign(Align.RIGHT);
		  multiRenderer.setYAxisMax(max+1);
		  multiRenderer.setAxisTitleTextSize(18);
		  multiRenderer.setLegendTextSize(18);
		  multiRenderer.setChartTitleTextSize(18);
		    multiRenderer.setLabelsTextSize(18);
		    multiRenderer.setYAxisMin(-0.5);
		    multiRenderer.setBarSpacing(0.5);
		    multiRenderer.setXLabels(0);
		   // multiRenderer.setYLabels(10);
		    multiRenderer.setMargins(new int[] {30, 70, 10, 0});
		
		   //multiRenderer.setZoomButtonsVisible(true);   
		    
		    for(int f= 0; f<fechas.length;f++)
		    {
		    	multiRenderer.addXTextLabel(f, fechas[f]);
		    }
		try
		{
		multiRenderer.addSeriesRenderer(xyestilo);
		graf = ChartFactory.getBarChartView(getApplicationContext(), dataset, multiRenderer, Type.DEFAULT);
		vista.addView(graf);
		
		}
		catch (Exception es)
		{
			Log.e("xxx", es.getMessage());
		}
		
	}
}
